@echo off
::::::::::::::::::: Check parameters :::::::::::::
if "%1" == "" (echo "Please specify an IIS node name (IIS 6 use ID)" & goto :EOF)
set "node_name=%1"
if "%2" == "" (echo "Please specify the direcrory that you want to switch to" & goto :EOF)
set "new_path=%2"
if "%3" == "" (echo "Please specify what version of IIS your targeting" & goto :EOF)
set "iis_version=%3"
set "remote_ip=%4"
set "remote_user=%5"
set "remote_password=%6"

:::::::::::::::::::: Main  :::::::::::::::::::::::
if "%remote_ip%" == "" (goto Remote) else (goto Local)
::::::::::::::::::::::::::::::::::::::::::::::::::

:Local
	echo Checking Remote IP: %remote_ip%
	ping -n 1 -l 1 %remote_ip% > nul
	if %errorlevel%==0 goto CopyToServer
	if %errorlevel%==1 goto NetworkError
	echo Should not get here
	Pause
goto :EOF

:Remote
	echo Waho I'm now on the remote server - Running swapnode
	call:swapnode %node_name% %new_path%
goto :EOF

:CopyToServer
	echo Transporting myself to remote server
	psexec.exe \\%remote_ip% -u %remote_user% -p %remote_password% -c iis-path-swap.bat %node_name% %new_path% %iis_version%
goto :EOF

:NetworkError
	echo Can not reach %2 (Is OpenVPN[192.168.110.x] or Hamachi[5.x.x.x] connected?)
goto :EOF

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:swapnode node_name new_path
::
:: By:   Courtenay Probert, updated 2010-12-23. Version 1.0
::
:: Func: Swaps the path is an IIS node
::
:: Args: %1 the name of the node (by ref)
::       %2 the full local path of the new directory (by ref)
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS

	if exist %new_path% goto Swap else goto Error
	
	:Swap
	echo Swaping IIS node node_name (%new_path%)
	
	if %iis_version% GEQ 7 (
		echo IIS %iis_version% is greater than or equal to 7
		C:\Windows\System32\inetsrv\appcmd set vdir "%node_name%/" -physicalPath:%new_path%
	) else (
		echo IIS %iis_version% is less than or equal to 7
		C:\Inetpub\AdminScripts\adsutil.vbs set /w3svc/%node_name%/root/path %new_path%
	)
	goto :EOF

	:Error 
	echo usage: iis-path-swap.bat node_name new_path
	echo error: Check %new_path% exists
	goto :EOF
	
endlocal
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


